package com.weslide.weslideapp.fragment;

import org.json.JSONException;

import com.gt.cl.http.CLConnectionException;
import com.gt.cl.http.CLInvalidNetworkException;
import com.weslide.weslideapp.R;
import com.weslide.weslideapp.activity.MainActivity_de;
import com.weslide.weslideapp.manager.api.APIManager;
import com.weslide.weslideapp.manager.api.LoginManager;
import com.weslide.weslideapp.pojo.ao.LoginAO;
import com.weslide.weslideapp.pojo.ao.RegisterAO;
import com.weslide.weslideapp.util.TMTPAsyncTask;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterFragment extends WeSlideFragment {
	private static final String TAG = "RegisterFragment";
	private EditText mEdtUserName;
	private EditText mEdtUserPassword1;
	private EditText mEdtUserPassword2;
	private EditText mEdtInvitationCode;
	private Button mBtnRegister;
	private LoginAO ao;

	private TMTPAsyncTask<Void, Void, Void> asyncTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		globalView = inflater.inflate(R.layout.page_register, container, false);
		findViews();
		return globalView;
	}

	private void findViews() {
		mEdtUserName = (EditText) findViewById(R.id.page_register_mEdtUserName);
		mEdtUserPassword1 = (EditText) findViewById(R.id.page_register_mEdtUserPassword1);
		mEdtUserPassword2 = (EditText) findViewById(R.id.page_register_mEdtUserPassword2);
		mEdtInvitationCode = (EditText) findViewById(R.id.page_register_mEdtInvitationCode);
		mBtnRegister = (Button) findViewById(R.id.page_register_mBtnRegister);
		mBtnRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (confirmation()) {
					register();
				}

			}
		});
	}

	private void register() {
		asyncTask = new TMTPAsyncTask<Void, Void, Void>(getActivity()) {

			@Override
			protected void doOnSuccess(Void result) {
				if (ao.getStatus().equals("1")) {
					LoginManager.getInstance().saveUserInformation(ao);

					MainFragment mainFragment = new MainFragment();
					Bundle bundle = new Bundle();
					bundle.putBoolean(MainActivity_de.EXTRA_IS_USERCENTER, true);
					mainFragment.setArguments(bundle);
					getGlobalNavigatrionFragment().push(mainFragment, true);
				} else {
					Toast.makeText(getActivity(), "注册失败", Toast.LENGTH_SHORT)
							.show();
				}
			}

			@Override
			protected Void doInBackground(Void params)
					throws CLInvalidNetworkException, CLConnectionException {
				String userName = mEdtUserName.getText().toString().trim();
				String pwd1 = mEdtUserPassword1.getText().toString().trim();
				String pwd2 = mEdtUserPassword2.getText().toString().trim();
				String invitationCode = mEdtInvitationCode.getText().toString()
						.trim();
				try {
					ao = APIManager.getInstance().register(userName, pwd1,
							invitationCode);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				return null;
			}
		};
		asyncTask.execute();
	}

	private boolean confirmation() {
		if (mEdtUserName.getText().toString().trim().equals("")) {
			Toast.makeText(getActivity(), "用户名不可为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (mEdtUserPassword1.getText().toString().trim().equals("")) {
			Toast.makeText(getActivity(), "密码不可为空", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!mEdtUserPassword1.getText().toString().trim()
				.equals(mEdtUserPassword2.getText().toString().trim())) {
			Toast.makeText(getActivity(), "两次密码不一致", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}
}
